package pers.ht.mbpo.service;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import pers.ht.mbpo.bean.*;
import pers.ht.mbpo.dao.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/RegServlet")
public class RegServlet extends HttpServlet {
    private MatchDao matchDao = new MatchDao();
    private UserDao userDao = new UserDao();
    private NoticeDao noticeDao = new NoticeDao();
    private CompetitorDao competitorDao = new CompetitorDao();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");
        if ("insert".equals(action)) {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String nickname = request.getParameter("nickname");
            String college = request.getParameter("college");
            String major = request.getParameter("major");
            userDao.insert(new User(username, password, nickname, college, major));
            request.getRequestDispatcher("RegServlet?action=query").forward(request, response);
        }
        if ("login".equals(action)) {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            boolean flag = true;
            String msg = "";
            UserDao userDao = new UserDao();
            User loginUser = userDao.queryOne(username);
            if (username.equals(loginUser.getUsername()) && password.equals(loginUser.getPassword())) {
                request.getSession().setAttribute("loginUser", loginUser);
                response.sendRedirect("RegServlet?action=query");
            } else if (loginUser.getUsername() == null) {
                flag = false;
                msg = "用户不存在！";
            } else {
                flag = false;
                msg = "用户名或密码错误，请重新输入！";
            }
            if (!flag) {
                request.setAttribute("msg", msg);
                request.setAttribute("username", username);
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if ("query".equals(action)) {
            User user = (User) request.getSession().getAttribute("loginUser");
            List<Match> matches = matchDao.query();
            for (Match match : matches) {

                int num = competitorDao.queryNum(match.getMatchId(), user.getUsername());


                if (matchDao.ifEnd(match.getEndTime()) && num == 1) {
                    match.setRemark("<a title=\"报名成功\" class=\"layui-btn layui-btn-disabled layui-btn-xs\" lay-event=\"\">报名成功</a>");
                } else if (matchDao.ifEnd(match.getEndTime())) {
                    match.setRemark("<a title=\"报名已结束\" class=\"layui-btn layui-btn-disabled layui-btn-xs\" lay-event=\"\">报名已结束</a>");
                } else if (num == 1) {
                    match.setRemark("<a title=\"取消报名\" class=\"layui-btn layui-btn layui-btn-xs\" lay-event=\"unSign\">取消报名</a>");
                } else if (matchDao.ifBegin(match.getBeginTime())) {
                    match.setRemark("<a title=\"报名未开始\" class=\"layui-btn layui-btn-disabled layui-btn-xs\" lay-event=\"\">报名未开始</a>");
                } else if (match.getNum() == match.getMaxNumber()) {
                    match.setRemark("<a title=\"人数已满\" class=\"layui-btn layui-btn-disabled layui-btn-xs\" lay-event=\"\">人数已满</a>");
                } else {
                    match.setRemark("<a title=\"报名\" class=\"layui-btn layui-btn layui-btn-xs\" lay-event=\"sign\">报名</a>");
                }

            }
            List<Notice> notices = noticeDao.query();
            String matchJson = JSONArray.fromObject(matches).toString();
            request.setAttribute("matchJson", matchJson);
            request.setAttribute("notices", notices);
            request.getRequestDispatcher("notice.jsp").forward(request, response);
        }
        if ("add".equals(action)) {
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }
        if ("sign".equals(action)) {
            String matchId = request.getParameter("matchId");
            User user = (User) request.getSession().getAttribute("loginUser");
            String username = user.getUsername();
            String msg = null;
            Match match = matchDao.queryOne(matchId);
            if (matchDao.ifBegin(match.getBeginTime())) {
                msg = "报名未开始";
            } else if (matchDao.ifEnd(match.getEndTime())) {
                msg = "报名已结束";
            }else if(match.getNum() == match.getMaxNumber()){
                msg = "人数已满";
            }else{
                competitorDao.insert(new Competitor(username, matchId));
            }
            request.setAttribute("msg", msg);
            request.getRequestDispatcher("RegServlet?action=query").forward(request, response);
        }
        if ("unSign".equals(action)) {
            String matchId = request.getParameter("matchId");
            User user = (User) request.getSession().getAttribute("loginUser");
            competitorDao.delete(user.getUsername(), matchId);
            request.getRequestDispatcher("RegServlet?action=query").forward(request, response);
        }
    }
}